package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.mta.pokerstats.Player;

/**
 * Servlet implementation class servletPlayer
 */
public class servletPlayer extends BaseRecordsAbstractServlet {
	private static final long serialVersionUID = 1L;
	
	private SimpleDateFormat m_DateFormatter = new SimpleDateFormat("yyyy-MM-dd");
	
	protected void processInsertRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		int playerID = 0;
		Player currPlayer;
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String name = request.getParameter("name");
		String bDayStr = request.getParameter("birthday");
		Date birthday = null;
		String joinStatus = request.getParameter("joinStatus");
		int teamID = Integer.parseInt(request.getParameter("teamID"));
		if (bDayStr != null && (bDayStr.length() > 0)){
			try {
				birthday = new Date(m_DateFormatter.parse(bDayStr).getTime());
			} catch (ParseException e) {
				birthday = null;
				e.printStackTrace();
			}
		}
		
		currPlayer = new Player(playerID, username, password, name,
				birthday, joinStatus,teamID);
		if (currPlayer.InsertToDatabase()) {
			out.print("{" + k_SuccessStr + " , \"playerID\" : \"" + currPlayer.getId() +"\"}");
		} else {
			out.print("{" + k_FailStr + "}");
		}
	}
	
	protected void processUpdateRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		int playerID = Integer.parseInt(request.getParameter("playerID"));
		Player currPlayer = Player.getPlayerById(playerID);
		String username = request.getParameter("username");
		currPlayer.setUsername(username);
		String password = request.getParameter("password");
		currPlayer.setPassword(password);
		String name = request.getParameter("name");
		currPlayer.setName(name);
		String joinStatus = request.getParameter("joinStatus");
		currPlayer.setJoinStatus(joinStatus);
		int teamID = Integer.parseInt(request.getParameter("teamID"));
		currPlayer.setTeamId(teamID);
		Date birthday;
		try {
			birthday = new Date(m_DateFormatter.parse(
					request.getParameter("birthday")).getTime());
		} catch (ParseException e) {
			birthday = new Date(new java.util.Date().getTime());
			e.printStackTrace();
		}
		currPlayer.setBirthday(birthday);
		if (currPlayer.UpdateInDatabase()) {
			out.print("{ " + k_SuccessStr + "}");

		} else {
			out.print("{" + k_FailStr + "}");
		}
	}
	
	protected void processDeleteRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		int playerID = Integer.parseInt(request.getParameter("playerID"));
		Player currPlayer = Player.getPlayerById(playerID);
		
		if (currPlayer.DeleteFromDatabase()) {
			out.print("{" + k_SuccessStr + "}");
		} else {
			out.print("{" + k_FailStr + "}");
		}
	}
	
	protected void processChangeJoinStatusRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		int playerID = Integer.parseInt(request.getParameter("playerID"));
		Player currPlayer = Player.getPlayerById(playerID);
		String newJoinStatus = request.getParameter("joinStatus");
		currPlayer.setJoinStatus(newJoinStatus);
		
		if (currPlayer.UpdateInDatabase()) {
			out.print("{" + k_SuccessStr + "}");
		} else {
			out.print("{" + k_FailStr + "}");
		}		
	}
	
	@Override
	protected void processExtraRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		String theAction = request.getParameter("action");
		if (theAction.equals("changeJoinStatus")){
			processChangeJoinStatusRequest(request, response);
		}
	}
}